﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using VMExplorer.Win32;

namespace VMExplorer.MemoryObjects
{
  class ListEntry : TypeWrapper<Native.LIST_ENTRY>
  {
    #region Private fields

    private SafeProcessHandle _hProcess;

    #endregion
    #region Construction

    public ListEntry (SafeProcessHandle hProcess, Native.LIST_ENTRY inner)
			: base (inner) 
    {
      _hProcess = hProcess;
    }

    #endregion
    #region Flat list creation

    public List<MemoryAddress> ForwardList ()
    {
			List<MemoryAddress> list = new List<MemoryAddress> ();

      MemoryAddress firstNext = Inner.Flink;
      MemoryAddress next = firstNext;

      do
      {
        list.Add (next);
        if (!next.IsValid) break;
        next = RemoteReader.ReadIntPtr (_hProcess, next);
      } while (next != firstNext);

      // The last index is the list header so remove it
      list.RemoveAt (list.Count - 1);

      return list;
    }

    #endregion
  };
}
